package service

import (
	"context"
	"database/sql"
	"fmt"
	"github.com/pkg/errors"
	"study-demo/tools"
	pb "study-demo/api"
)

//获取详情
func (s *Service) GetProductDetail(ctx context.Context, req *pb.Request) (reply *pb.Reply, err error) {
	quary_map, err := tools.JsonStrToMap(req.Params)
	if err != nil {
		return tools.ReplayError(-1, err, "参数解析失败")
	}
	productId := int64(quary_map["productId"].(float64))
	product_data, err := s.dao.GetProductDetail(ctx, productId)

	if errors.Cause(err) == sql.ErrNoRows {
		fmt.Printf("product not found, %v\n", err)
		return tools.ReplayOK(product_data, "获取数据成功")
	}

	if err != nil {
		return tools.ReplayError(500, err, "获取数据失败")
	}
	return tools.ReplayOK(product_data, "获取数据成功")
}